var lastReceived = 0;
var isWait = false;

var fetch = function () {
    if (isWait) return;
    isWait = true;
    $.getJSON("/lp/fetch?lastReceived=" + lastReceived, function (data) {
        if (data == null) return;
        var i=0;
        var fnish=false;
        $.each(data, function (i, event) {
            var li = document.createElement('li');
            switch (event.Type) {
            // case 0: // JOIN
            //     if (event.User == $('#uname').text()) {
            //         li.innerText = 'You joined the chat room.';
            //     } else {
            //         li.innerText = event.User + ' joined the chat room.';
            //     }
            //     break;
            // case 1: // LEAVE
            //     li.innerText = event.User + ' left the chat room.';
            //     break;
            case 2: // MESSAGE
                var username = document.createElement('strong');
                var content = document.createElement('span');

                username.innerText = event.User;
                content.innerText = event.Content;

                li.appendChild(username);
              //  li.appendChild(document.createTextNode(': '));

             //   console.log(content.innerText);
               var content= JSON.parse(event.Content);
                if(content.status=="Pull complete"){
                    fnish=true;
                }else if(content.status=="Downloading"){
                    $(".Fnish").hide();
                    $(".noFnish").show();
                }else if(content.status="Status: Image is up to date for registry.cn-shenzhen.aliyuncs.com/uniappadmin/base_service:latest"){
                    fnish=true;
                }
                var div =$('<li class="ant-timeline-item"><div class="ant-timeline-item-tail"></div><div class="ant-timeline-item-head ant-timeline-item-head-blue"></div><div class="ant-timeline-item-content">'+event.Content+'</div></li>');
                // div = $('<div class="item">123123<div class="c">X</div></div>');
                //.appendChild(content);
                if($('#chatbox').children().length >10){
                    $('#chatbox').children().remove();
                }
                $('#chatbox').append(div);
              //  li.appendChild(content);
                 i++;
                break;
            }

            // $('#images li:nth-child(3)').css('background','#107');
            if(fnish==true){
                $(".Fnish").show();
                $(".noFnish").hide();
                $(".Install").hide();
                $("#grid").hide();
            }

            lastReceived = event.Timestamp;
        });
        isWait = false;
    });
}

// Call fetch every 3 seconds
setInterval(fetch, 3000);

fetch();

$(document).ready(function () {

    var postConecnt = function () {
        var uname = $('#uname').text();
        var content = $('#sendbox').val();
        $.post("/lp/post", {
            uname: uname,
            content: content
        });
        $('#sendbox').val("");
    }

    $('#sendbtn').click(function () {
        $(this).content="正在安装中...........";
        $(".noInstall").hide()
        $(".Install").show()
        postConecnt();
        Uniappadmins(100,100)
    });
});




